package com.cloudcc.mobile.im.ui;

import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.cloudcc.mobile.R;
import com.cloudcc.mobile.im.UIControl;
import com.easemob.chat.EMChatManager;
import com.easemob.chat.EMConversation;
import com.easemob.easeui.ui.EaseConversationListFragment;
import com.easemob.easeui.utils.EaseUserUtils;
import com.easemob.util.NetUtils;

/**
 * 会话列表
 * 
 * @author seven
 * 
 */
public class ConversationListFragment extends EaseConversationListFragment {

	private TextView errorText;

	@Override
	protected void initView() {
		super.initView();
		View errorView = (LinearLayout) View.inflate(getActivity(),
				R.layout.em_chat_neterror_item, null);
		errorItemContainer.addView(errorView);
		errorText = (TextView) errorView.findViewById(R.id.tv_connect_errormsg);
	}

	@Override
	protected void setUpView() {
		super.setUpView();
		// 注册上下文菜单
		registerForContextMenu(conversationListView);
		conversationListView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				EMConversation conversation = conversationListView
						.getItem(position);
				String username = conversation.getUserName();
				if (username.equals(EMChatManager.getInstance()
						.getCurrentUser()))
					Toast.makeText(getActivity(),
							R.string.Cant_chat_with_yourself, 0).show();
				else {
					// 进入聊天页面
					String userNickName = EaseUserUtils.getUserNickName(username);
					UIControl.openChatUI(getActivity(),username,userNickName, conversation.isGroup());
				}
			}
		});
	}

	@Override
	protected void onConnectionDisconnected() {
		super.onConnectionDisconnected();
		if (NetUtils.hasNetwork(getActivity())) {
			errorText.setText(R.string.can_not_connect_chat_server_connection);
		} else {
			errorText.setText(R.string.the_current_network);
		}
	}

	@Override
	public void onCreateContextMenu(ContextMenu menu, View v,
			ContextMenuInfo menuInfo) {
		getActivity().getMenuInflater().inflate(R.menu.em_delete_message, menu);
	}

	@Override
	public boolean onContextItemSelected(MenuItem item) {
		boolean handled = false;
		boolean deleteMessage = false;
		/*
		 * if (item.getItemId() == R.id.delete_message) { deleteMessage = true;
		 * handled = true; } else
		 */if (item.getItemId() == R.id.delete_conversation) {
			deleteMessage = true;
			handled = true;
		}
		EMConversation tobeDeleteCons = conversationListView
				.getItem(((AdapterContextMenuInfo) item.getMenuInfo()).position);
		// 删除此会话
		EMChatManager.getInstance().deleteConversation(
				tobeDeleteCons.getUserName(), tobeDeleteCons.isGroup(),
				deleteMessage);
		refresh();


		return handled ? true : super.onContextItemSelected(item);
	}

}
